/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graph;
import java.util.*;
import java.io.RandomAccessFile;
import java.io.File;
import structures.*;
/**
 *
 * @author Gonzalo
 */
public class Schema {
    public String dir;
    public RandomAccessFile schema;
    public RandomAccessFile nodes;
    public Texts texts;
    public HashMap<String,Edges> edges;
    public HashMap<String,Attributes> attributes;


    public Schema(String _dir){
        dir=_dir;
        edges=new HashMap<String,Edges>();
        attributes=new HashMap<String,Attributes>();
        String label;
        //Nodes
        try{
            File folder = new File(dir);
            if(!folder.exists())
                folder.mkdirs();
            schema=new RandomAccessFile(dir+"/_esq.sch","rw");
            nodes=new RandomAccessFile(dir+"/_nodes.sch","rw");
            texts=new Texts(dir+"/_text.sch");

            label=schema.readLine();
            String t[];
            while(label!=null){
                t=label.split(" ");
                label=t[0];
                if(t[1].trim().compareTo("edge")==0){
                    edges.put(label, new Edges(new Disk(dir+"/"+label+".gdb",8),new Disk(dir+"/"+label+".gdb.aux",128)));
                    edges.put(inverso(label), new Edges(new Disk(dir+"/"+inverso(label)+".gdb",8),new Disk(dir+"/"+inverso(label)+".gdb.aux",128)));
                }else{
                    attributes.put(label, new Attributes(t[1],new Disk(dir+"/"+label+".attr",2),new Disk(dir+"/"+inverso(label)+".attr",8),new Disk(dir+"/"+inverso(label)+".attr.aux",32)));
                }
                label=schema.readLine();
            }

        }catch(Exception e){}
        //Labels             
    }

    public boolean createEdgeType(String label){
        if(edges.containsKey(label)) return false;
        try{
            schema.writeBytes(label+" edge\r\n");
            edges.put(label, new Edges(new Disk(dir+"/"+label+".gdb",8),new Disk(dir+"/"+label+".gdb.aux",16)));
            edges.put(inverso(label), new Edges(new Disk(dir+"/"+inverso(label)+".gdb",8),new Disk(dir+"/"+inverso(label)+".gdb.aux",16)));
        }catch(Exception e){
            return false;
        }
        return true;
    }

    public boolean createAttributeType(String label,String type){
        if(attributes.containsKey(label)) return false;
        try{
            schema.writeBytes(label+" "+type+"\r\n");
            attributes.put(label, new Attributes(type,new Disk(dir+"/"+label+".attr",2),new Disk(dir+"/"+inverso(label)+".attr",8),new Disk(dir+"/"+inverso(label)+".attr.aux",32)));
        }catch(Exception e){
            return false;
        }
        return true;
    }

    public Graph getGraph(){
        return new Graph(nodes,edges,attributes,texts);
    }

    public void close(){
        try{
            schema.close();
            nodes.close();
            texts.close();
        }catch(Exception e){
            
        }
        Iterator<Edges> it=edges.values().iterator();
        while(it.hasNext())
            it.next().close();
        Iterator<Attributes> ita=attributes.values().iterator();
        while(ita.hasNext())
            ita.next().close();
    }

    public String inverso(String label){return label+"_";}
}
